<?php

    header('content-type:text/html;charset="utf-8') ;
    
    $host = 'localhost:3306';
    $name = 'root';
    $pwd = 'root';
    $dbname = 'test2';
    $conn = mysqli_connect($host , $name , $pwd , $dbname);

    if(!$conn) {
        die('数据库连接失败');
    }



    // 前端传第几页，显示几条数据
    $page = $_POST['page'] ;   
    $showSize = $_POST['showSize'] ;  
    $type = $_POST['type'] ;


    // 0,5  5,5  10,5    (page-1) * 5
    $start = ($page - 1) * $showSize ;


    $obj = [] ;


    if($type) {
        $sql1 = "select count(*) pageAll from goods_list where goods_type = '$type'" ;
        $sql2 = "select * from goods_list where goods_type = '$type' limit $start , $showSize ";
    } else {
        $sql1 = "select count(*) pageAll from goods_list" ;
        $sql2 = "select * from goods_list limit $start , $showSize";
    }


 
    $res = mysqli_query($conn , $sql1) ;
    $arr = mysqli_fetch_array($res) ;
    $pageAll = $arr['pageAll'] ;


    

   
    // 默认只会转第一条数据
    $res = mysqli_query($conn , $sql2) ;

    $list = [] ;

    while($data = mysqli_fetch_array($res)) {
        $temp = [] ;
        $temp['id'] = $data['goods_id'] ;
        $temp['name'] = $data['goods_name'] ;
        $temp['title'] = $data['goods_title'] ;
        $temp['des'] = $data['goods_des'] ;
        $temp['img'] = $data['goods_img'] ;
        $temp['price'] = $data['goods_price'] ;
        $temp['type'] = $data['goods_type'] ;
        array_push($list , $temp) ;
    }

    if($list) {
        $obj['status'] = true ;
        $obj['msg'] = '数据请求成功';
        $obj['data'] = $list ;
        $obj['pageAll'] = $pageAll ;
    } else {
        $obj['status'] = false ;
        $obj['msg'] = '服务器错误';
        $obj['data'] = $list ;
    }

    echo(json_encode($obj , JSON_UNESCAPED_UNICODE));